import type { Reducer, Effect } from 'umi';
import { history } from 'umi';

import { getStatusMap } from '@/services/common';
import { formatStatusMap } from '@/utils/tools';

export type StateType = {
  status?: any;
};

export type LoginModelType = {
  namespace: string;
  state: StateType;
  effects: {
    getStatusMap: Effect;
  };
  reducers: {
    setStatusMap: Reducer<StateType>;
  };
};

const Model: LoginModelType = {
  namespace: 'common',

  state: {
    status: {},
  },

  effects: {
    *getStatusMap({ payload }, { call, put }) {
      const response = yield call(getStatusMap, payload);
      const lastData = formatStatusMap(response?.data);
      yield put({
        type: 'setStatusMap',
        payload: lastData,
      });
    },
  },

  reducers: {
    setStatusMap(state, { payload }) {
      return {
        ...state,
        status: payload,
      };
    },
  },
};

export default Model;
